package com.patri.tema1;

import java.io.InputStream;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;



public class Testxml4 extends Activity {
	
	int[] iduri= {R.id.a1,R.id.a3, R.id.a2, R.id.b1,R.id.b3, R.id.b2, R.id.c1, R.id.c3,R.id.c2, R.id.d1,R.id.c3,R.id.d2};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testxml);
        //TextView tv=(TextView)findViewById(R.id.tv);
        TextView data=(TextView)findViewById(R.id.data);
        ImageView vr1=(ImageView)findViewById(R.id.vr1);
        TextView vrazi=(TextView)findViewById(R.id.vrazi);
        vr1.setImageResource(R.drawable.sunny);
        //tv.setText("bgf ");
        //Log.d("patri", tv.getText().toString());
        
        try {
        	 
    		//File fXmlFile = new File("C:\\Users\\Patrisha\\Documents\file.xml");
        	InputStream fXmlFile = getAssets().open("file"+ ".xml");
        	Intent intent= getIntent();
        	URL url= new URL("http://www.google.com/ig/api?weather="+intent.getStringExtra("numeoras"));
        	
        	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(new InputSource(url.openStream()));
			//Document doc = db.parse(fXmlFile);
			doc.getDocumentElement().normalize();
     
    		//tv.setText(tv.getText()+"Root element :" + doc.getDocumentElement().getNodeName());
    		NodeList nList = doc.getElementsByTagName("forecast_information");
    		for (int temp = 0; temp < nList.getLength(); temp++) {
     
    		   Node nNode = nList.item(temp);
    		   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
    			  Element eElement = (Element) nNode;
    			 
    		      
    		      NodeList websiteList = eElement.getElementsByTagName("forecast_date");
    		      Element websiteElement = (Element) websiteList.item(0);
    		      websiteList = websiteElement.getChildNodes();
    		      data.setText("Data : "+websiteElement.getAttribute("data"));
    		      
    		      
    		      
    		   }
    		
    		}
    		nList = doc.getElementsByTagName("current_conditions");
    		for (int temp = 0; temp < nList.getLength(); temp++) {
     
    		   Node nNode = nList.item(temp);
    		   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
    			  Element eElement = (Element) nNode;
    			 
    		      
    		      NodeList websiteList = eElement.getElementsByTagName("condition");
    		      Element websiteElement = (Element) websiteList.item(0);
    		      websiteList = websiteElement.getChildNodes();
    		      vrazi.setText("Condition : "+websiteElement.getAttribute("data"));
    		      
    		      websiteList = eElement.getElementsByTagName("temp_c");
    		      websiteElement = (Element) websiteList.item(0);
    		      websiteList = websiteElement.getChildNodes();
    		      vrazi.setText(vrazi.getText()+"Temperature "+websiteElement.getAttribute("data")+"\n");
    		      
    		       websiteList = eElement.getElementsByTagName("humidity");
    		      websiteElement = (Element) websiteList.item(0);
    		      websiteList = websiteElement.getChildNodes();
    		      vrazi.setText(vrazi.getText()+" "+websiteElement.getAttribute("data")+"\n");
    		   
    		       websiteList = eElement.getElementsByTagName("condition");
    		      websiteElement = (Element) websiteList.item(0);
    		      websiteList = websiteElement.getChildNodes();
    		      vrazi.setText(vrazi.getText()+" "+websiteElement.getAttribute("data")+"\n");
    		      
    		      websiteList = eElement.getElementsByTagName("wind_condition");
    		      websiteElement = (Element) websiteList.item(0);
    		      websiteList = websiteElement.getChildNodes();
    		      vrazi.setText(vrazi.getText()+" "+websiteElement.getAttribute("data"));
    		   }
    		
    		}
    		nList = doc.getElementsByTagName("forecast_conditions");
    		for (int temp = 0; temp < nList.getLength(); temp++) {
     
    		   Node nNode = nList.item(temp);
    		   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
    			  Element eElement = (Element) nNode;
    			  NodeList websiteList = eElement.getElementsByTagName("day_of_week");
    		      Element websiteElement = (Element) websiteList.item(0);
    		      websiteList = websiteElement.getChildNodes();
    		      //tv.setText(tv.getText()+"Day : "+websiteElement.getAttribute("data"));
    		      TextView zi=(TextView)findViewById(iduri[3*temp]);
    		      zi.setText(""+websiteElement.getAttribute("data"));
    		      
    			  websiteList = eElement.getElementsByTagName("low");
    		      websiteElement = (Element) websiteList.item(0);
    		      websiteList = websiteElement.getChildNodes();
    		      //tv.setText(tv.getText()+"Low : "+websiteElement.getAttribute("data"));
    		      int t=(Integer.parseInt(websiteElement.getAttribute("data"))-32)*5/9;
    		      zi=(TextView)findViewById(iduri[3*temp+2]);
    		      zi.setText("Temperature:"+t+"\n");
    		      
    		      websiteList = eElement.getElementsByTagName("condition");
    		      websiteElement = (Element) websiteList.item(0);
    		      websiteList = websiteElement.getChildNodes();
    		      zi.setText(zi.getText()+"Condition: "+websiteElement.getAttribute("data")+ "\n");
    		      
    		      
    		      ImageView vr2=(ImageView)findViewById(iduri[3*temp+1]);
    		       
    		      vr2.setImageResource(R.drawable.sunny);
    		   } 
    		  
    		
    		}
    		   
    	  } catch (Exception e) {
    		e.printStackTrace();
    	  }
      }
     
     
    }
